
class TopDownModule(object):
    def __init__(self, actorMngr):
        self.aActors = actorMngr.aActors;
        self.aActive_Actors = actorMngr.aActive_Actors;
        self.nFrame = 0;
        self.actor = None;
        
    def process_Actor(self, actor, nFrame):
        self.actor = actor;
        self.nFrame = nFrame;
#         self.clearActor_Bias(actor);
        self.check_ApproachMeet();
        self.check_WaitMeet1();
        self.check_WaitMeet2();
        self.check_WalkTogether();
    
    def check_ApproachMeet(self):
        approachMeet = self.actor.activityMngr.approachAndMeet;
        for nOtherTrackId in approachMeet:
            if not(approachMeet[nOtherTrackId].bStarted): continue;
            otherActor = self.aActors[nOtherTrackId];
            self.setActor_Bias(otherActor, approachMeet[nOtherTrackId], "walk", [1, 1]);
    
    def check_WaitMeet1(self):
        waitMeet1 = self.actor.activityMngr.waitAndMeet1;
        loiter = self.actor.activityMngr.loiter;
        if waitMeet1==None or not(waitMeet1.bActive): return;
        aPos = self.getActorPos(self.actor);
        nX, nY, nR = aPos[0], aPos[1], loiter.nMaxDist;
        self.actor.aROI_Persons.append([[nX-nR, nY-nR], [nX+nR, nY+nR]]);
    
    def check_WaitMeet2(self):
        waitAndMeet2 = self.actor.activityMngr.waitAndMeet2;
        for nOtherTrackId in waitAndMeet2:
            if not(waitAndMeet2[nOtherTrackId].bStarted): continue;
            otherActor = self.aActors[nOtherTrackId];
            self.setActor_Bias(otherActor, waitAndMeet2[nOtherTrackId], "walk", [1, 1]);
    
    def check_WalkTogether(self):
        walkTogether = self.actor.activityMngr.walkTogether;
        for nOtherTrackId in self.actor.activityMngr.walkTogether:
            if not(walkTogether[nOtherTrackId].bStarted): continue;
            otherActor = self.aActors[nOtherTrackId];
            self.setActor_Bias(self.actor, walkTogether[nOtherTrackId], "walk", [1, 1]);
            self.setActor_Bias(otherActor, walkTogether[nOtherTrackId], "walk", [1, 1]);
#             print self.nFrame, (self.actor.nTrackId, nOtherTrackId), [self.actor.nPerson_Bias, self.aActors[nOtherTrackId].nPerson_Bias]; 
    
    def setActor_Bias(self, actor, activity, sAction, aWeights):
        nWeight_Activity, nWeight_Action = aWeights[0], aWeights[1];
        nPerson_Bias = (1-actor.nPerson_Score) * (activity.nProb*nWeight_Activity);
        nAction_Bias = 0;
        if actor.sAction==sAction:
            nAction_Bias = (1-actor.nAction_Score) * (activity.nProb*nWeight_Action);
        if nPerson_Bias>actor.nPerson_Bias: actor.nPerson_Bias = nPerson_Bias;
        if nAction_Bias>actor.nAction_Bias: actor.nAction_Bias = nAction_Bias;
            
    def clearActor_Bias(self, actor):
        actor.aROI_Persons = [];
        actor.nPerson_Bias = 0;
        actor.nAction_Bias = 0;
        
    def getActorPos(self, actor):
        [x1, y1] = actor.minPosBB;
        [x2, y2] = actor.maxPosBB;
        return actor.getPos(x1, y1, x2, y2);